﻿using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Net;
using com.evaair.mall;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Configuration;

/// <summary>
/// 資料驗證
/// </summary>
public class Validations
{

    /// <summary>
    /// 驗證手機條碼Code39 (第一碼為/其餘Code39 0~9 A~Z + - .)
    /// </summary>
    /// <param name="strNumber">要驗證的字串</param>
    /// <returns>是否通過驗證</returns>
    public static bool IsMobileCode39(string strValue)
    {
       B2C_MALL_HOME ws = new B2C_MALL_HOME();
       ws.Url = ConfigurationManager.AppSettings["B2C_MALL_HOME"].ToString();
       string strJson = ws.B2C_MALL_EINVOICE(CommonUtil.GetWebDevice(), "", strValue);
       JObject jObj = JsonConvert.DeserializeObject<JObject>(strJson);
       return (jObj["Is_Valid"].ToString() == "Y" & jObj["isExist"].ToString() == "Y");
      //return new Regex(@"^/[A-Z0-9+\-.\s]{7}$").IsMatch(strValue);

    }

    /// <summary>
    /// 驗證自然人憑證(前2碼大寫英文字母後14碼數字)
    /// </summary>
    /// <param name="strNumber">要驗證的字串</param>
    /// <returns>是否通過驗證</returns>
    public static bool IsPINCode (string strValue)
    {
        return new Regex(@"^[A-Z]{2}[0-9]{14}$").IsMatch(strValue);
    }

    /// <summary>
    /// 驗證Email
    /// </summary>
    /// <param name="input">要驗證的字串</param>
    /// <returns>是否通過驗證</returns>
    public static bool IsEmail(string strValue)
    {
        return new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$").IsMatch(strValue);
    }

    /// <summary>
    /// 驗證電話號碼
    /// </summary>
    /// <param name="input">要驗證的字串</param>
    /// <returns>是否通過驗證</returns>
    public static bool IsTelephone(string strValue)
    {
      return new Regex(@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?").IsMatch(strValue);
    }

    /// <summary>
    /// 驗證由數字組成的字串
    /// </summary>
    /// <param name="input">要驗證的字串</param>
    /// <returns>是否通過驗證</returns>
    public static bool IsNumeric(string strValue)
    {
      return new Regex("^[0-9]+$").IsMatch(strValue);
    }

    /// <summary>
    /// 驗證身份證號碼
    /// </summary>
    /// <param name="input">要驗證的字串</param>
    /// <returns>是否通過驗證</returns>
    public static bool IsTWID(string strValue)
    {
      bool isCheck = true;
      int[] intID = new int[10];    //數字陣列存放身分證字號用
      int intTotal;               //計算總和用

      if (strValue.Length == 10)    //檢查長度
      {
          strValue = strValue.ToUpper();    //將身分證字號英文改為大寫

          //將輸入的值存入陣列中
          for (int i = 1; i < strValue.Length; i++)
          {
              intID[i] = Convert.ToInt32(strValue.Substring(i, 1));
          }
          //將開頭字母轉換為對應的數值
          switch (strValue.Substring(0, 1).ToUpper())
          {
              case "A": intID[0] = 10; break;
              case "B": intID[0] = 11; break;
              case "C": intID[0] = 12; break;
              case "D": intID[0] = 13; break;
              case "E": intID[0] = 14; break;
              case "F": intID[0] = 15; break;
              case "G": intID[0] = 16; break;
              case "H": intID[0] = 17; break;
              case "I": intID[0] = 34; break;
              case "J": intID[0] = 18; break;
              case "K": intID[0] = 19; break;
              case "L": intID[0] = 20; break;
              case "M": intID[0] = 21; break;
              case "N": intID[0] = 22; break;
              case "O": intID[0] = 35; break;
              case "P": intID[0] = 23; break;
              case "Q": intID[0] = 24; break;
              case "R": intID[0] = 25; break;
              case "S": intID[0] = 26; break;
              case "T": intID[0] = 27; break;
              case "U": intID[0] = 28; break;
              case "V": intID[0] = 29; break;
              case "W": intID[0] = 32; break;
              case "X": intID[0] = 30; break;
              case "Y": intID[0] = 31; break;
              case "Z": intID[0] = 33; break;
          }
          //檢查第一個數值是否為1.2(判斷性別)
          if (intID[1] == 1 || intID[1] == 2)
          {
              intTotal = (intID[0] / 10 * 1) + (intID[0] % 10 * 9);

              int k = 8;
              for (int j = 1; j < 9; j++)
              {
                  intTotal += intID[j] * k;
                  k--;
              }

              intTotal += intID[9];

              if (intTotal % 10 != 0)
              {
                isCheck = false;
              }
          }
          else
          {
            isCheck = false;
          }
      }
      else
      {
        isCheck = false;
      }

      return isCheck;
    }

}